package com.lyncs.ods.modules.share.controller;


import cn.hutool.core.util.IdUtil;
import com.lyncs.ods.common.api.CommonResult;
import com.lyncs.ods.common.exception.ApiException;
import com.lyncs.ods.common.web.RequestHolder;
import com.lyncs.ods.constant.LyncsOdsConstant;
import com.lyncs.ods.modules.company.model.CompanyInvitationLog;
import com.lyncs.ods.modules.company.service.CompanyInvitationLogService;
import com.lyncs.ods.modules.share.ShareStrategyFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * <p>
 * 公司账号信息表 前端控制器
 * </p>
 *
 * @author alex
 * @since 2022-02-15
 */
@RestController
@Api(tags = {"邀请相关接口"})
public class ShareController {

    @Autowired
    private CompanyInvitationLogService companyInvitationLogService;

    /**
     * create a share link
     *
     * @return url
     */
    @PostMapping("{type}/link")
    @ApiOperation(value = "创建链接")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型", dataType = "String", paramType = "path", allowableValues = "order,deliver,settlement,company,user"),
            @ApiImplicitParam(name = "invited_company_id", value = "受邀企业ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "数据(json字符串)", dataType = "String", paramType = "query"),
    })
    public CommonResult<String> createShareLink(HttpServletRequest request, @PathVariable String type, @RequestParam("invited_company_id") Long invitedCompanyId, @RequestParam("data") String data) {
        LyncsOdsConstant.ShareType t = Arrays.stream(LyncsOdsConstant.ShareType.values()).filter(i -> i.getValue().equals(type)).findFirst().orElse(null);
        if (t == null) {
            throw new ApiException("do not support type of '" + type + "'");
        }
        String key = IdUtil.nanoId(6);
        CompanyInvitationLog log = new CompanyInvitationLog()
                .setCompanyId(RequestHolder.getCompanyId())
                .setInvitedCompanyId(invitedCompanyId)
                .setType(t.getKey())
                .setKey(key)
                .setData(data)
                .setSendLogId(null)
                .setExpireTime(null)
                .setStatus(1)
                .setCreator(RequestHolder.getUserId())
                .setCreateTime(RequestHolder.getRequestDateTime())
                .setUpdateTime(LocalDateTime.now());
        companyInvitationLogService.save(log);
        String path = request.getScheme() + "://" + request.getServerName() + type + key;
        return CommonResult.success(path);
    }

    /**
     * accept share
     *
     * @return T/F
     */
    /*@PostMapping("{type}/{key}")
    @ApiOperation(value = "接受邀请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型", dataType = "String", paramType = "path", allowableValues = "order,deliver,settlement,company,user"),
            @ApiImplicitParam(name = "key", value = "key", dataType = "String", paramType = "path"),
    })
    public CommonResult<Object> acceptShareLink(@PathVariable String type, @PathVariable String key) {
        return CommonResult.success(ShareStrategyFactory.exec(type, key));
    }*/

}

